package org.kyw.org.service.impl;

import org.kyw.basic.exception.BusinessException;
import org.kyw.basic.service.impl.BaseServiceImpl;
import org.kyw.org.domain.Employee;
import org.kyw.org.domain.Shop;
import org.kyw.org.mapper.EmployeeMapper;
import org.kyw.org.mapper.ShopMapper;
import org.kyw.org.service.IEmployeeService;
import org.kyw.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }

        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if(employee != null){
            throw new BusinessException("账号已经存在，请直接登录，如果忘记密码，请走找回密码流程");
        }

        //employeeMapper.save(admin);
        employeeService.add(admin);

        shop.setAdmin(admin);
        shopMapper.save(shop);

        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
